<template>
	<view class="main">
		<!-- <image class="center" mode="widthFix" src="../../static/loading_logo.png"></image> -->
		<image class="bottom" mode="widthFix" src="../../static/loading_bottom.png"></image>
		<yomol-upgrade :type="upgradeType" :url="upgradeUrl" title="发现新版本" :content="upgradeContent" ref="yomolUpgrade"></yomol-upgrade>
	</view>

</template>

<script>
	var util = require('../../utils/util.js');
	var api = require('../../utils/prom.js');
	import yomolUpgrade from '@/components/yomol-upgrade/yomol-upgrade.vue'
	export default {
		components: {
			yomolUpgrade
		},
		onLaunch: function() {
			console.log('App Launch')
		},
		data() {
			return {
				value: '这是一个示例',
				/* 版本更新 */
				upgradeType: 'pkg', //pkg 整包 wgt 升级包
				upgradeContent: '', //更新内容
				upgradeUrl: '', //更新地址
			}
		},
		methods: {
			checkVersionClick(callback) {
				// 检测升级
				//#ifdef H5  
				console.log("这段代码编译到H5平台");
				callback()
				//#endif
				// #ifndef H5  
				console.log("这段代码编译到非H5平台");

				plus.runtime.getProperty(plus.runtime.appid, (widgetInfo) => {
					var platform = uni.getSystemInfoSync().platform
					var type = 1
					if (platform == 'android') {
						type = 1
					} else {
						type = 2
					}
					//根据当前平台去后台检测版本号
					//这是我的逻辑，自己自定义
					console.log(type)
					if(type==1){
						//android
						 
						// uni.request({
						// 	url: 'https://api.zrxff.com/zrxapi/v1/sys/version?type='+type,
						// 	success: (res) => {
						// 		console.log(res)
						// 		console.log(res.data.version.Fversion)
						// 		if (res.data.version.Fversion != widgetInfo.version) {
						// 			this.upgradeType = 'pkg'
						// 			this.upgradeContent = '部分逻辑优化'
						// 			this.upgradeUrl = res.data.version.Fpath
						// 			this.$refs.yomolUpgrade.show()
						// 		} else {
						// 			callback()
						// 		}
						// 	},
						// 	fail: (error) => {
						// 		console.log(error);
						// 		callback()
						// 	}
						// });
					}else{
						//ios
						callback()
					}
				
				});
				// #endif

			},
			Login: function() {
				const token = uni.getStorageSync('token')
				let loginForm = uni.getStorageSync('loginForm')
				console.log(loginForm)

				if (loginForm.mobile && loginForm.password) {

					let data = {
						mobile: loginForm.mobile,
						password: loginForm.password, //md5(this.password),
						code: '',
						loginType: 1
					};
					uni.request({
						// url: 'https://api.zrxff.com/api/v1/user/login',
						url: 'https://api.zrxff.com/zrxapi/v1/user/login',
						method: 'POST',
						data: data,
						header: {
							'Content-Type': 'application/x-www-form-urlencoded'
						},
						success: (res) => {
							console.log(res)
							if (res.data.errno == 0) {

								uni.setStorageSync('token', res.data.Token)
								uni.setStorageSync('smsnum', res.data.smsnum)
								uni.setStorageSync('loginForm', {
									mobile: data.mobile,
									password: data.password,
								})
								uni.setStorageSync('userInfo', res.data.User)
								uni.setStorageSync('smsnum', res.data.smsnum)
								console.log(res.data.Token)
								var platform = uni.getSystemInfoSync().platform
								var type = 1
								if (platform == 'android') {
									type = 1
								} else {
									type = 2
								}
								console.log('cid', uni.getStorageSync('cid'))
								// 检测升级
								//#ifdef H5  
								console.log("这段代码编译到H5平台");
						uni.reLaunch({
							url: '../main/main'
						})
								//#endif
								// #ifndef H5  
								var cid = uni.getStorageSync('cid')
								this.$api.setToken({
									"Appos": type,
									"deviceno": cid
								}).then(res => {
									console.log(res)
									uni.reLaunch({
										url: '../main/main'
									})

								}).catch((err) => {
									console.log('err', err)
								})
								//#endif


							} else {
								uni.reLaunch({
									url: '../login/login'
								})
								uni.showToast({
									title: res.data.errmsg,
								})
							}

						}

					})

				} else {

					uni.reLaunch({
						url: '../login/login'
					})
				}
			}
		},
		onShow: function() {
			console.log("app show")
			
			// 检测升级
			//#ifdef H5  
			console.log("这段代码编译到H5平台");
			this.Login()
			//#endif
			// #ifndef H5  
			// 获取设备CID  
			var pinf = null;
			var cid = null;
			var times = null;
			times = setInterval(() => {
				pinf = plus.push.getClientInfo();
				cid = pinf.clientid; //客户端标识  
				console.log(cid)
				if (cid != '' && cid != null && cid != undefined && cid != 'undefined' && cid != 'null') {
					console.log(cid)
					clearInterval(times)
					if (cid) {
						uni.setStorageSync('cid', cid)
						this.checkVersionClick(() => {
							this.Login()
						})

					}

				}
			}, 1000)
			//#endif
		},
		onHide: function() {
			console.log('App Hide')
		},
	}
</script>

<style>
	.main {
		width: 100%;
		height: 100vh;
		overflow: hidden;
		background: #fff;
	}

	.main .center {
		position: absolute;
		top: 30%;
		width: 100%;
	}

	.main .bottom {
		position: absolute;
		bottom: 0;
		width: 100%;
	}
</style>
